import { createRouter,createWebHistory} from "vue-router";
import Layout from "../components/layout/index.vue";
import FrontLayout from "../views/front_desk/Home.vue"
//路由信息
const routes = [
    {
        path: '/',
        redirect: "/main",
    },
    {
        path: '/home',
        name: 'Home',
        component: () => import('../views/front_desk/Home.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/LoginRegister.vue')
    },
    {
        path: '/adminLogin',
        name: 'AdminLogin',
        component: () => import('../views/adminLogin.vue')
    },
    {
        path: '/main',
        component: Layout,
        redirect: "/house",
    },
    {
        path: '/house',
        name: "房屋管理",
        component: Layout,
        children: [
            {
                path: "house-list",
                name: "HouseList",
                component: () => import("../views/home/HouseList.vue"),
            },
            {
                path: "/house-info",
                name: "房屋详情",
                component: () => import("../views/home/HouseInfo.vue"),
            }
        ]
    },
    {
        path: '/user',
        name: "用户管理",
        component: Layout,
        children: [
            {
                path: "user-list",
                name: "UserList",
                component: () => import("../views/home/UserList.vue"),
            },
            {
                path: "user-setting",
                name: "UserSetting",
                component: () => import("../views/home/UserSetting.vue"),
            }
        ]
    },
    {
        path: '/family',
        name: "家庭管理",
        component: Layout,
        children: [
            {
                path: "family-list",
                name: "FamilyList",
                component: () => import("../views/home/FamilyList.vue"),
            }
        ]
    },
    {
        path: '/work',
        name: "工作管理",
        component: Layout,
        children: [
            {
                path: "work-list",
                name: "WorkList",
                component: () => import("../views/home/WorkList.vue"),
            }
        ]
    },
    {
        path: '/subsidy',
        name: "补贴管理",
        component: Layout,
        children: [
            {
                path: "subsidy-list",
                name: "SubsidyList",
                component: () => import("../views/home/SubsidyList.vue"),
            }
        ]
    },
    {
        path: '/reservation',
        name: "预约管理",
        component: Layout,
        children: [
            {
                path: "reservation-list",
                name: "ReservationList",
                component: () => import("../views/home/ReservationList.vue"),
            }
        ]
    },
    {
        path: '/front_home',
        name: "前台管理",
        component: FrontLayout,
        children: [
            {
                path: "subsidy",
                name: "Subsidy",
                component: () => import("../views/front_desk/FrontSubsidy.vue"),
            },
            {
                path: "work",
                name: "Work",
                component: () => import("../views/front_desk/FrontWork.vue"),
            },
            {
                path: "apply",
                name: "Apply",
                component: () => import("../views/front_desk/ApplyFor.vue"),
            },
            {
                path: "me",
                name: "Me",
                component: () => import("../views/front_desk/FrontMe.vue"),
            }
        ]
    },
    {
        path: '/:catchAll(.*)',
        name: '/404',
        component: () => import('../views/404.vue')
    }
];
// 导出路由
const router = createRouter({
    history: createWebHistory(),
    routes
});
router.beforeEach((to, from, next) => {
    // to是将要访问的路径
    // from:代表从哪个路径跳转而来
    // next:是一个函数，放行继续
    if (to.path==="/login" || to.path==="/adminLogin") return next();
    //获取token
    const token = localStorage.getItem("token");
    if(!token) return next("/login")
    next()
})
export default router;